from django import forms
from django.core.validators import RegexValidator
from day06.utils.encrypt import md5


class LoginForm(forms.Form):
    """ 用户名登录 form组件，校验表单信息 """
    role = forms.ChoiceField(
        label="用户类型",
        choices=(("2", "客户"), ("1", "管理员")),
        widget=forms.Select(attrs={"class": "form-control"})
    )
    username = forms.CharField(
        label="用户名",
        widget=forms.TextInput(attrs={"class": "form-control", "placeholder": "用户名"})
    )
    password = forms.CharField(
        max_length=12,
        min_length=4,
        label="密码",
        widget=forms.PasswordInput(attrs={"class": "form-control", "placeholder": "密码"})
    )

    def clean_password(self):
        return md5(self.cleaned_data['password'])


class SmsLoginForm(forms.Form):
    """ 短信验证码登录 form组件, 校验表单信息 """
    role = forms.ChoiceField(
        label="用户类型",
        choices=(("2", "客户"), ("1", "管理员")),
        widget=forms.Select(attrs={"class": "form-control"})
    )

    mobile = forms.CharField(
        label="手机号",
        validators=[RegexValidator(r'1[358]\d{9}$', '手机号格式错误'), ],
        widget=forms.TextInput(attrs={"class": "form-control", "placeholder": "手机号"})
    )

    code = forms.CharField(
        label="短信验证码",
        validators=[RegexValidator(r'^[0-9]{4}$', '验证码格式错误'), ],
        widget=forms.TextInput(attrs={"class": "form-control", "placeholder": "短信验证码"})
    )


class MobileForm(forms.Form):
    role = forms.ChoiceField(
        label="用户类型",
        choices=(("2", "客户"), ("1", "管理员")),
        widget=forms.Select(attrs={"class": "form-control"})
    )

    mobile = forms.CharField(
        label="手机号",
        validators=[RegexValidator(r'1[358]\d{9}$', '手机号格式错误'), ],
        required=True
    )
